jQuery(function ($) {
    $('#issueRobUserCountPie,#issueRobUserAgePie').css('margin-top',-266 + 'px');
    initSelectYear(4);
    $('#mostWeekSelect').change(function () {
        initIssueRobWeekUser();
    });
    $('#monthUserSelect').change(function () {
        initMonthUserTotal();
    });
    $('#mostUserSelect').change(function () {
        initIssueRobMostUser();
    });
    $('#monthUserSexSelect').change(function () {
        initMonthUserSexTotal();
    });
    $('#issueRobAgeSectionSelect').change(function () {
        initIssueRobAgeSectionPie();
    });
    initMonthUserTotal();
    initIssueRobUserTotal();
    initIssueRobWeekUser();
    initIssueRobMostUser();
    initMonthUserSexTotal();
    initIssueRobAgeSectionPie();
});

/**
 * 初始化本年/上年每个月下单/抢单的用户数
 */
function initMonthUserTotal(){
    $.ajax({
        type: "POST",
        url: '/userOrderChart/listMonthUserTotal?thisYear=' + $('#monthUserSelect').val(),
        dataType:'json',
        cache: true,
        success: function(monthUserData){
            drawMonthUserTotalChart(monthUserData);
        }
    });
}
/**
 * 画initMonthUserTotal（本年/上年每个月下单/抢单的用户数）折线图
 */
function drawMonthUserTotalChart(monthUserData) {
    let muLegendArr = [],monthArr = [], muSeriesArr = [];
    for (let g = 0; g < monthUserData.length; g ++){
        let muObj = monthUserData[g], monthData = [], irStr = muObj.issueOrRobFlag == 'i' ? '下' : '抢';
        let lineName = muObj.myYear + irStr + '单';
        muLegendArr.push(lineName);
        for (let m in muObj){
            let isMonth = m != 'myYear' && m != 'issueOrRobFlag';
            if (isMonth){
                monthData.push(muObj[m] == null ? 0 : muObj[m]);
            }
            if (g == 0 && isMonth){
                monthArr.push(m + '月');
            }
        }
        let onObj = {
            name: lineName,
            type: 'line',
            areaStyle: {},
            data: monthData
        }
        muSeriesArr.push(onObj);
    }
    drawStackedLineChart('本/上年每月下/抢单用户数',muLegendArr,monthArr,muSeriesArr,'monthUserChart',38,'个');
}

/**
 * 初始化从未下过单、未抢过单、抢过单未下过单、下过单未抢过单、既下单又抢单、下过单、抢过单等情况下的用户数量
 */
function initIssueRobUserTotal(){
    let irCodeArr = ['issueNoRob','issueUser','noIssue','noIssueRob','noRob','robNoIssue','robUser'],
        irNameArr = ['发未抢用户','发布用户','未发布用户','未发布未抢用户','未抢用户','抢未发布用户','抢单用户'],
    irUserArr = [];
    $.ajax({
        type: "POST",
        url: '/userOrderChart/listIssueRobUserTotal',
        dataType:'json',
        cache: true,
        success: function(irUserData){
            let irUserObj = irUserData[0];
            for (let p in irUserObj) {
                for (let g = 0; g < irCodeArr.length; g ++){
                    if (p == irCodeArr[g]){
                        irUserArr.push({name: irNameArr[g],value: irUserObj[p]})
                    }
                }
            }
            drawCommonPie('各下单抢单用户情况','用户数及占比',irUserArr,'issueRobUserCountPie');
        }
    });
}

/**
 * 初始化订单最多的6个城市及订单总量
 */
function initIssueRobWeekUser(){
    $.ajax({
        type: "POST",
        url: '/userOrderChart/listIssueRobWeekUser?thisYear=' + $('#mostWeekSelect').val(),
        dataType:'json',
        cache: true,
        success: function(mostWeekUserData){
            drawIssueRobWeekUser(mostWeekUserData);
        }
    });
}

/**
 * 获取配置中series数组里的对象
 * @param seriesName series名称
 * @param seriesDataArr series数据数组
 * @returns series对象
 */
function getSeriesObj(seriesName,seriesDataArr) {
    return {
        name: seriesName,
        type: 'bar',
        barGap: 0,
        label: defaultLabelOption,
        data: seriesDataArr//数据数组
    };
}
/**
 * 画订单最多的6个城市及订单总量柱状图
 * @param mostOrderCityData
 */
function drawIssueRobWeekUser(mostWeekUserData) {
    let muLegendArr = [],weekArr = [], muSeriesArr = [];
    for (let g = 0; g < mostWeekUserData.length; g ++){
        let muObj = mostWeekUserData[g], monthData = [], irStr = muObj.issueOrRobFlag == 'i' ? '下' : '抢';
        let lineName = muObj.myYear + irStr;
        muLegendArr.push(lineName);
        for (let m in muObj){
            let isMonth = m != 'myYear' && m != 'issueOrRobFlag';
            if (isMonth){
                monthData.push(muObj[m] == null ? 0 : muObj[m]);
            }
            if (g == 0 && isMonth){
                weekArr.push(getDayWeek(m));
            }
        }
        let onObj = {
            name: lineName,
            type: 'line',
            data: monthData
        }
        muSeriesArr.push(onObj);
    }
    drawStackedLineChart('下/抢单用户数',muLegendArr,weekArr,muSeriesArr,'mostWeekUser',28,'个');
}

/**
 * 初始化2年内发布和抢单最多的6个用户
 */
function initIssueRobMostUser() {
    $.ajax({
        type: "POST",
        url: '/userOrderChart/listIssueRobMostUser?thisYear=' + $('#mostUserSelect').val(),
        dataType:'json',
        cache: true,
        success: function(mostUserData){
            drawIssueRobMostUser(mostUserData);
        }
    });
}

/**
 * 画initIssueRobMostUser（今年和去年订单量最多的6个用户）柱状图
 * @param mostData 订单数据
 */
function drawIssueRobMostUser(mostUserData) {
    let thisArr = [], lastArr = [], chartIdArr = ['lastMostUser','thisMostUser'],yAxisMax = 0;
    for (let g = 0; g < mostUserData.length; g ++){
        let tlObj = mostUserData[g];
        //如果是今年的数据
        if (mostUserData[g].sort == 1){
            thisArr.push(tlObj);
        } else {
            lastArr.push(tlObj);
        }
        //如果某个统计值 > 坐标轴的最大值
        if (tlObj.yearTotal > yAxisMax){
            yAxisMax = tlObj.yearTotal;
        }
    }
    let r = yAxisMax % 50, d = parseInt(yAxisMax / 50);
    yAxisMax = (r > 0 ? (d + 1) : d) * 50;
    for (let i = 0; i < 2; i ++){
        //如果是今年的数据
        let isLast = i == 0;
        let tlArr = isLast ? lastArr : thisArr, tlLegendArr = [],tlSeriesArr = [], tlXAxisArr = [];
        tlXAxisArr.push(tlArr[0].issueRobUser);
        for (let j = 0; j < tlArr.length; j ++){
            let tlObj = tlArr[j], seriesDataArr = [];
            seriesDataArr.push(tlObj.yearTotal);
            if (isLast){
                seriesDataArr.push('');
            }
            tlLegendArr.push(tlObj.userId);
            let seriesObj = getSeriesObj(tlObj.userId,seriesDataArr);
            tlSeriesArr.push(seriesObj);
        }
        if (isLast){
            tlXAxisArr.push('');
        }
        let tlTitle = isLast ? '近2年下/抢订单最多的前6名用户' : '';
        let tlOption = getRotationOptionObj(tlTitle,tlLegendArr,tlXAxisArr,tlSeriesArr,yAxisMax,isLast,false,'个');
        setChartOption(tlOption,chartIdArr[i]);
    }
}

/**
 * 初始化每个月下单/抢单男女用户的数量
 */
function initMonthUserSexTotal(){
    $.ajax({
        type: "POST",
        url: '/userOrderChart/listMonthUserSexTotal?thisYear=' + $('#monthUserSexSelect').val(),
        dataType:'json',
        cache: true,
        success: function(monthSexData){
            drawMonthUserSexTotal(monthSexData);
        }
    });
}
/**
 * 画initMonthUserSexTotal（每个月下单/抢单男女用户的数量）函数的折线图
 */
function drawMonthUserSexTotal(monthSexData) {
    let sexLegendArr = ['男下','女下','未知下','男抢','女抢','未知抢'],monthArr = [],
        monthData = [[],[],[],[],[],[]], sexSeriesArr = [];
    for (let g = 0; g < monthSexData.length; g ++){
        let sexObj = monthSexData[g], irStr = sexObj.issueOrRobFlag == 'i' ? '下' : '抢';
        if (irStr == '下'){
            monthArr.push(sexObj.orderMonth + '月');
            monthData[0].push(sexObj.maleNum);//男下
            monthData[1].push(sexObj.femaleNum);//女下
            monthData[2].push(sexObj.unknowSexNum);//未知下
        }else {
            monthData[3].push(sexObj.maleNum);//男抢
            monthData[4].push(sexObj.femaleNum);//女抢
            monthData[5].push(sexObj.unknowSexNum);//未知抢
        }
    }
    for (let g = 0; g < sexLegendArr.length; g ++){
        sexSeriesArr.push({
            name: sexLegendArr[g],
            type: 'line',
            data: monthData[g]
        });
    }
    drawStackedLineChart('下单/抢单男女用户数',sexLegendArr,monthArr,sexSeriesArr,'monthUserSexTotal',32,'个');
}
/**
 * 初始化下单/抢单用户各年龄段的数量和比例
 */
function initIssueRobAgeSectionPie(){
    $.ajax({
        type: "POST",
        url: '/userOrderChart/listIssueRobAgeSection?thisYear=' + $('#issueRobAgeSectionSelect').val(),
        dataType:'json',
        cache: true,
        success: function(ageSectionData){
            drawIssueRobAgeSectionPie(ageSectionData);
        }
    });
}
/**
 * 画initIssueRobAgeSectionPie（下单/抢单用户各年龄段的数量和比例）饼状图
 */
function drawIssueRobAgeSectionPie(ageSectionData) {
    let ageSection = ageSectionData[0],issueAgeArr = [],robAgeArr = [];
    for (let age in ageSection){
        let n = age.split('_')[1],v = ageSection[age];
        //如果是发布用户
        if (age.indexOf('i') >= 0){
            issueAgeArr.push({name: n,value: v});
        } else {
            robAgeArr.push({name: n,value: v});
        }
    }
    let option = {
        title: {
            text: '下(外环)/抢(内圆)单年龄段用户数/比例',
            x: 'center',
            left: 'left'
        },
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b}: {c} ({d}%)"
        },
        series: [
            {
                name:'抢单用户',
                type:'pie',
                center: ['50%', '60%'],
                selectedMode: 'single',
                radius: [0, '44%'],
                label: {
                    normal: {
                        position: 'inner'
                    }
                },
                data: robAgeArr
            },
            {
                name:'下单用户',
                type:'pie',
                center: ['50%', '60%'],
                radius: ['60%', '75%'],
                data: issueAgeArr
            }
        ]
    };
    setChartOption(option,'issueRobUserAgePie');
}
